#!/usr/bin/env python

appdata = [
{'appid': 'xxx','siteKey': 'xxx'},
]

import os
#XXX It is your job to write a script to update the app.
#you can see /wallproxy/update.sh
rootpath = os.path.abspath(os.path.join('.','wallproxy'));
updateshpath = os.path.abspath(os.path.join(rootpath,'update.sh'))
appyamlpath = os.path.abspath(os.path.join(rootpath,'app.yaml'))
passwordpath = os.path.abspath(os.path.join(rootpath,'password.py'))

default = {
'siteKey'     : 'default wallproxy public key!',
'cfgCacheTime': 5*60,
'cacheTime'   : 24*3600,
'maxSize'     : 900*1000,
'cryptoMode'  : 'AES-CBC-32',
 }
def writeappid(appid):
  f = open(appyamlpath,'r');
  appyamldata = f.read();
  f.close()
  p = appyamldata.find('application: ')
  end = appyamldata[p:].find('\n')
  appyamldata = appyamldata[:p+len('application: ')]+appid+appyamldata[end:]
  f = open(appyamlpath,'w');
  f.seek(0)
  f.write(appyamldata);
  f.close()

def writepassword(data):
  str1 = '';
  for x in data.keys():
    if (x=='appid'):continue;
    if isinstance(data[x],str):
      str1 += x+' = u"'+data[x]+'"\n';
    if isinstance(data[x],int):
      str1 += x+' = '+str(data[x])+'\n';
  f = open(passwordpath,'w')
  f.write(str1);
  f.close();

def update(data):
  if not('appid' in data):
    print 'please tell me the appid.',data;
    return;
  for x in default.keys():
    if not(x in data):
      data[x]=default[x];
  writeappid(data['appid']);
  writepassword(data)
  os.system(updateshpath);

def main():
  os.chdir(rootpath);
  os.system('ls')
  global appdata
  for x in appdata:
    update(x);

if __name__ == '__main__':
  main()
